
#define _XOPEN_SOURCE

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include "utils.h"
#include <time.h>

void closeC(int logVar){		//Close the client file
if(logVar==2)		//Only close if was logging to file
    fclose(clientFile);
}

void closeS(int logVarS){		//Close the server file
if(logVarS==2)		//Only close if was logging to file
    fclose(serverFile);
}

int sendall(const int sock, const char *buf, const size_t len)
{
        size_t tosend = len;
        while (tosend > 0) {
                ssize_t bytes = send(sock, buf, tosend, 0);
                if (bytes <= 0) 
                        break; // send() was not successful, so stop.
                tosend -= (size_t) bytes;
                buf += bytes;
        };

        return tosend == 0 ? 0 : -1;
}

int recvline(const int sock, char *buf, const size_t buflen)
{
        int status = 0; // Return status.
        size_t bufleft = buflen;

        while (bufleft > 1) {
                // Read one byte from scoket.
                ssize_t bytes = recv(sock, buf, 1, 0);
                if (bytes <= 0) {
                        // recv() was not successful, so stop.
                        status = -1;
                        break;
                } else if (*buf == '\n') {
                        // Found end of line, so stop.
                        *buf = 0; // Replace end of line with a null terminator.
                        status = 0;
                        break;
                } else {
                        // Keep going.
                        bufleft -= 1;
                        buf += 1;
                }
        }
        *buf = 0; // add null terminator in case it's not already there.

        return status;
}


int process_config_line(char *line, struct config_params *params)
{
        // Ignore comments.
        if (line[0] == CONFIG_COMMENT_CHAR)
                return 0;

        // Extract config parameter name and value.
        char name[MAX_CONFIG_LINE_LEN];
        char value[MAX_CONFIG_LINE_LEN];
        int items = sscanf(line, "%s %s\n", name, value);

        // Line wasn't as expected.
        if (items != 2)
                return -1;

        // Process this line.
        if (strcmp(name, "server_host") == 0) {
                strncpy(params->server_host, value, sizeof params->server_host);
        } else if (strcmp(name, "server_port") == 0) {
                params->server_port = atoi(value);
        } else if (strcmp(name, "username") == 0) {
                strncpy(params->username, value, sizeof params->username);
        } else if (strcmp(name, "password") == 0) {
                strncpy(params->password, value, sizeof params->password);
        }// else if (strcmp(name, "data_directory") == 0) {
        //      strncpy(params->data_directory, value, sizeof params->data_directory);
        //} 
        else {
                // Ignore unknown config parameters.
        }

        return 0;
}


int read_config(const char *config_file, struct config_params *params)
{
        int error_occurred = 0;

        // Open file for reading.
        FILE *file = fopen(config_file, "r");
        if (file == NULL)
                error_occurred = 1;

        // Process the config file.
        while (!error_occurred && !feof(file)) {
                // Read a line from the file.
                char line[MAX_CONFIG_LINE_LEN];
                char *l = fgets(line, sizeof line, file);

                // Process the line.
                if (l == line)
                        process_config_line(line, params);
                else if (!feof(file))
                        error_occurred = 1;
        }

        return error_occurred ? -1 : 0;
}

void logger(FILE *file, char *message, int doLogging)	//Logger
{
    if(doLogging == 0){	//If don't log, do nothing
        return;
    }
    else if(doLogging == 1){	//If logVar = 1, print to screen
        printf("%s", message); fflush(stdout);
    }
    else if(doLogging == 2){	//Else if 2, write to file
        fprintf(file,"%s",message);
        fflush(file);
    }
}

char *generate_encrypted_password(const char *passwd, const char *salt)
{
        if(salt != NULL)
                return crypt(passwd, salt);
        else
                return crypt(passwd, DEFAULT_CRYPT_SALT);
}



